<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Currency_entity{
	var $id = 0;
	var $code = "";
	var $name = "";
	var $rate = 0;
	var $flag = "";
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_CURRENCY)->result();
		}
		else if($cmd == 'code')
		{
			$result = $ci->db->where('code',$input)->get(TBL_CURRENCY)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->code = $result[0]->code;
			$this->name = $result[0]->name;
			$this->rate = $result[0]->rate;
			$this->flag = $result[0]->flag;
			
			// Create currency folder if not exists
			$directory = "./uploads/default/currency";
			if(!file_exists($directory)){
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			
			$directory = "./uploads/default/currency/".$result[0]->id ;
			if (!file_exists ( $directory )) {
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			
			return $this;
		}
		return false;
	}
	
	function setCode($input){ $this->code=$input; }
	function setName($input){ $this->name=$input; }
	function setRate($input){ $this->rate=$input; }
	function setFlag($input){ $this->flag=$input; }
	
	function getId(){ return $this->id; }
	function getCode(){ return $this->code; }
	function getName(){ return $this->name; }
	function getRate(){ return $this->rate; }
	function getFlag(){ return $this->flag; }
	
	function getFolderFileStorage(){ 
		return "./uploads/default/currency/".$this->id."/"; 
	}
	
	function getFileFlag(){
		return $this->getFolderFileStorage().$this->getFlag();
	}
	
	function getUSDFlag(){
		return "./uploads/default/currency/usd.jpg"; 
	}
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('code', strtoupper($this->code) );
		$ci->db->set('name', $this->name);
		$ci->db->set('rate', $this->rate);
		$ci->db->set('flag', $this->flag);
		
		$ci->db->where('id', $this->id)->update(TBL_CURRENCY);
	}
	
	function createNewEntity(){
		$ci=& get_instance();
		
		$ci->db->set('name', '');
		$ci->db->insert(TBL_CURRENCY);
		
		return $ci->db->insert_id();
	}
	
	function getListCurrency(){
		$ci=& get_instance();
		
		return $ci->db->get(TBL_CURRENCY)->result();
	}
	
}
